﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SSN.Utilities.Cache;
using SSN.Utilities.Database;
using SSN.DotNet;

namespace SSN.Utilities.Common
{
    /// <summary>
    /// Contains name and code of all states
    /// </summary>
    public static class StateCodes
    {
        private static ReadOnlyKeyValueCache<string, string> _cache = new ReadOnlyKeyValueCache<string, string>( getValues );

        private static Dictionary<string, string> getValues( )
        {
            Dictionary<string, string> ret = new Dictionary<string, string>( );
            using ( var db = DbManager.BossDb )
            {
                using ( var dr = db.QueryFast( "SELECT * FROM codes where codegroup='06' AND codetype='1' " ) )
                {
                    while ( dr.Read( ) )
                    {
                        ret.Add( dr[ "codeid" ].ToString( ), dr[ "codedesc" ].ToString( ) );
                    }
                }
            }
            return ret;
        }

        /// <summary>
        /// Returns a list of all state codes and names
        /// </summary>
        /// <returns>Read only dictionary</returns>
        public static ReadOnlyDictionary<string, string> GetAll( )
        {
            return _cache.AllItems;
        }

        /// <summary>
        /// Returns the name of the state with the given code
        /// </summary>
        /// <param name="code">State code</param>
        /// <returns>State name</returns>
        public static string GetStateName( string code )
        {
            if ( _cache.ContainsKey( code ) )
            {
                return _cache[ code ];
            }
            return string.Empty;
        }
    }
}
